<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>构成特定和需要添加的最少元素</title>
</head>
<body>
  <script>
    function fn(nums, limit, goal) {
      let all = nums.reduce((a, b) => a + b)
      if (all === goal) return 0
      let diff = Math.abs(goal - all)
      return Math.ceil(diff / limit)
    }
    console.log(fn([1, -1, 1], 3, -4))
  </script>
</body>
</html>